package com.magic.smartcity.view.news;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.magic.smartcity.R;
import com.magic.smartcity.beans.PressBean;
import com.magic.smartcity.databinding.FragmentNewsDetailBinding;
import com.magic.smartcity.utils.Constant;
import com.magic.smartcity.view.MainActivity;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;

public class NewsDetailFragment extends Fragment {

    private FragmentNewsDetailBinding mBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = FragmentNewsDetailBinding.bind(inflater.inflate(R.layout.fragment_news_detail, null));
        ButterKnife.bind(mBinding.getRoot());

        initView();
        initEvent();
        return mBinding.getRoot();
    }

    private void initView() {

        Bundle extras = getArguments();
        int id = extras.getInt("id");
        PressBean.RowsBean press = (PressBean.RowsBean) extras.getSerializable("press");
        MainActivity activity = (MainActivity) (getActivity());
        activity.setActionBarTitle(press.getTitle() + "");
        Picasso.get().load(Constant.Service_IP + press.getCover()).into(mBinding.image);

        mBinding.webview.loadData(press.getContent(), "text/html", "utf-8");

        mBinding.createTime.setText("发布时间: " + press.getCreateTime() + "");
        mBinding.readNum.setText("阅读量: " + press.getReadNum() + "");
        mBinding.commentNum.setText(press.getCommentNum() + " 条评论");

    }

    private void initEvent() {
        mBinding.btnSubmitComment.setOnClickListener(v -> {
            mBinding.edSubmitComment.setText("");
            Toast.makeText(getActivity().getBaseContext(), "评论成功!", Toast.LENGTH_SHORT).show();
        });
    }


}